﻿using Ledict.ConditionExtract.BLL;
using Ledict.ConditionExtract.Common;
using Ledict.ConditionExtract.Model;
using Ledict.Core.Data;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ledict.ConditionExtract
{
    class UpdateData
    {
        readonly NewIDBLL newid = new NewIDBLL();
        readonly CaseCauseBLL casecausebLL = new CaseCauseBLL();

        public void Update()
        {
            //先获取所有的案由类型的节点
            try
            {
                string sql = string.Empty;
                sql = " select * from SYS_CaseCause where CC_NodeType=2 ";
                Database oBase = DatabaseFactory.CreateDatabase("JudgeDoc");
                DbCommand oCmd = oBase.GetSqlStringCommand(sql);
                DataTable OtB = oBase.ExecuteDataSet(oCmd).Tables[0];
                if (OtB.Rows.Count > 0)
                {
                    foreach (DataRow dr in OtB.Rows)
                    {
                        string ccid = dr["CC_ID"].ToString();
                        string allParentName = dr["AllParentName"].ToString();
                        string allParentId = dr["AllParentId"].ToString();
                        string ccName = dr["CC_Name"].ToString();
                        //判断该案由下是否包含这些要素
                        sql = $" select *from SYS_CaseCause where CC_ParentID='{ccid}' and (CC_Name like'%诉请要素%' or CC_Name like'%事实要素%' or CC_Name like'%基本要素%'or CC_Name like'%结果要素%'or CC_Name like'%理由要素%'or CC_Name like'%争议焦点要素%') ";
                        oCmd = oBase.GetSqlStringCommand(sql);
                        DataTable OtBNew = oBase.ExecuteDataSet(oCmd).Tables[0];
                        if (OtBNew != null && OtBNew.Rows.Count > 0)
                        {
                            Log.WriteLog("开始处理案由节点："+ ccid);

                            //有数据则先添加案由要素节点
                            var model = new SYS_CaseCause();
                            model.CC_ID = newid.JudgeDoc_CreateID("SYS_CaseCause");
                            model.CC_Name = "案由要素";
                            model.CC_ParentID = ccid;
                            model.CC_NodeType = 11;
                            model.PIS_ShowName = "案由要素";
                            model.AllParentId = dr["AllParentId"].ToString() + "," + model.CC_ID;
                            model.AllParentName = dr["AllParentName"].ToString() + "," + model.CC_Name;
                            model.HW_level = model.AllParentName.Split(',').Length;
                            model.CC_IsProcessNode = 0;
                            model.CC_IsCombination = 0;
                            model.CC_CommonCase = 0;
                            model.CC_SerialNumb = 0;
                            model.XmlPath = dr["XmlPath"].ToString() + "/" + model.CC_Name;
                            string ccIdNew = casecausebLL.Exists(model.CC_Name, model.CC_ParentID);
                            if (!string.IsNullOrEmpty(ccIdNew))
                            {
                                continue;
                            }
                            else
                            {
                                if (casecausebLL.AddSYS_CaseCause(model, "syb"))
                                {

                                    foreach (DataRow dr1 in OtBNew.Rows)
                                    {
                                        string ccId1 = dr1["CC_ID"].ToString();
                                        var mode = casecausebLL.GetCaseCauseByCCID(ccId1);
                                        mode.CC_ParentID = model.CC_ID;
                                        string allparentN = mode.AllParentName.Replace(allParentName, model.AllParentName);
                                        string allparentI = mode.AllParentId.Replace(allParentId, model.AllParentId);
                                        string xmlP = mode.XmlPath.Replace("/" + ccName, "/案由要素");
                                        //先修改子级节点
                                        DataTable dtAll = casecausebLL.GetAllChild(ccId1);
                                        if (dtAll != null && dtAll.Rows.Count > 0)
                                        {
                                            foreach (DataRow dtAr in dtAll.Rows)
                                            {
                                                var mode1 = casecausebLL.GetCaseCauseByCCID(dtAr["CC_ID"].ToString());
                                                mode1.AllParentName = mode1.AllParentName.Replace(mode.AllParentName, allparentN);
                                                mode1.AllParentId = mode1.AllParentId.Replace(mode.AllParentId, allparentI);
                                                mode1.XmlPath = mode1.XmlPath.Replace(mode.XmlPath, xmlP);
                                                casecausebLL.UpdateSYS_CaseCause(mode1, "syb");

                                            }
                                        }
                                        //然后修改父级节点
                                        mode.AllParentName = allparentN;
                                        mode.AllParentId = allparentI;
                                        mode.XmlPath = xmlP;
                                        casecausebLL.UpdateSYS_CaseCause(mode, "syb");
                                    }
                                    Log.WriteLog("案由节点处理完成：" + ccid);

                                }
                            }
                        }
                    }
                    System.Windows.Forms.MessageBox.Show("节点处理完成！");
                }
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show("处理数据出错："+ex.Message);
            }
        }
    }
}
